package com.quan.weixin.web.controller.boss;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.weixin.common.WeixinContext;
import com.quan.weixin.rpc.api.WeixinApiService;
import com.quan.weixin.rpc.api.WeixinQrCodeService;
import com.quan.weixin.rpc.model.WeixinQrCode;

@RestController
@RequestMapping(value = "/api/weixin/qrcode")
public class WeixinQrCodeController extends BaseController {

	@Autowired
	private WeixinQrCodeService weixinQrCodeService;
	
	@Autowired
	private WeixinApiService apiService ;

	@RequiresPermissions(value="weixin:qrcode:add", description="添加微信二维码")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinQrCode entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		String qrCodeUrl = null ;
		//临时二维码
		if(null != entity.getQrCodeType() && entity.getQrCodeType() == 1) {
			qrCodeUrl = this.apiService.showQRCodeTemp(WeixinContext.getAccessToken(), entity.getExpireSeconds(), "QR_STR_SCENE", entity.getSceneValue()) ;
		} else {
			qrCodeUrl = this.apiService.showQRCodeForever(WeixinContext.getAccessToken(), "QR_LIMIT_STR_SCENE", entity.getSceneValue()) ;
		}
		entity.setQrCodeUrl(qrCodeUrl);
		entity.setComputerExpireSeconds((entity.getExpireSeconds() * 1000) + System.currentTimeMillis());
		this.weixinQrCodeService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:qrcode:delete", description="删除微信二维码")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinQrCodeService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="weixin:qrcode:delete", description="删除微信二维码")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.weixinQrCodeService.deleteByIds(ids) ;
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="weixin:qrcode:delete_expire", description="删除所有过期的临时二维码")
	@GetMapping(value = "/deleteExpire")
	public Object deleteExpire(String ids) {
		this.weixinQrCodeService.deleteByExpire();
		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:qrcode:update", description="修改微信二维码")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinQrCode entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinQrCodeService.update(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:qrcode:detail", description="查询微信二维码详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinQrCodeService.selectById(id);
	}
	
	@RequiresPermissions(value="weixin:qrcode:list", description="查询微信二维码")
	@GetMapping(value = "/list")
	public Object list(WeixinQrCode entity) {
		return this.weixinQrCodeService.selectPage(new Paginator(request), entity) ;
	}
	
	@RequiresPermissions(value="weixin:qrcode:updateTemplate", description="修改二维码管理模板")
	@PostMapping(value = "/updateTemplate")
	public Object updateTemplate(@RequestBody WeixinQrCode entity) {
		this.weixinQrCodeService.updateSelective(entity) ;
		return BaseResult.ok();
	}
	
}
